﻿using System;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace FileMergerApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSelectFile1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    txtFile1.Text = openFileDialog.FileName;
                }
            }
        }

        private void btnSelectFile2_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    txtFile2.Text = openFileDialog.FileName;
                }
            }
        }

        private void btnSelectFolder_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog folderDialog = new FolderBrowserDialog())
            {
                if (folderDialog.ShowDialog() == DialogResult.OK)
                {
                    txtFolder.Text = folderDialog.SelectedPath;
                }
            }
        }

        private void btnMerge_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtFile1.Text) || string.IsNullOrEmpty(txtFile2.Text))
                {
                    MessageBox.Show("请先选择两个文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (string.IsNullOrEmpty(txtFolder.Text))
                {
                    MessageBox.Show("请选择保存文件夹", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                string content1 = File.ReadAllText(txtFile1.Text, Encoding.UTF8);
                string content2 = File.ReadAllText(txtFile2.Text, Encoding.UTF8);

                string outputPath = Path.Combine(txtFolder.Text, "merged.txt");
                File.WriteAllText(outputPath, content1 + Environment.NewLine + content2, Encoding.UTF8);

                lblStatus.Text = $"文件合并成功！保存路径：{outputPath}";
                MessageBox.Show("文件合并完成！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                lblStatus.Text = "错误：" + ex.Message;
                MessageBox.Show($"操作失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}